package com.busionline.wotu.woochenandroid.Fragment;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.busionline.wotu.bannerview.BannerView;
import com.busionline.wotu.bannerview.adapter.BannerAdapter;
import com.busionline.wotu.baselibrary.adapter.LoadMoreRecycleView;
import com.busionline.wotu.baselibrary.mvp.fragment.BaseMvpFragment;
import com.busionline.wotu.baselibrary.mvp.proxy.InjectPresenter;
import com.busionline.wotu.woochenandroid.R;
import com.busionline.wotu.woochenandroid.adapter.HomeAdapter;
import com.busionline.wotu.woochenandroid.bean.ArticleBean;
import com.busionline.wotu.woochenandroid.bean.BannerBean;
import com.busionline.wotu.woochenandroid.contract.HomeContract;
import com.busionline.wotu.woochenandroid.presenter.HomePresenter;
import com.busionline.wotu.woochenandroid.tools.ImageLoadUtil;
import com.busionline.wotu.woochenandroid.tools.ToastUtil;
import com.busionline.wotu.woochenandroid.views.ContainerLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页
 *
 * @author woochen123
 * @time 2018/5/15 11:59
 * @desc
 */

public class HomeFragment extends BaseMvpFragment implements HomeContract.IHomeView {
    @BindView(R.id.recyclerView)
    LoadMoreRecycleView recyclerView;
    @BindView(R.id.containerLayout)
    ContainerLayout containerLayout;
    @BindView(R.id.refreshLayout)
    SwipeRefreshLayout refreshLayout;

    @InjectPresenter
    HomePresenter mHomePresenter;
    private BannerView mBannerView;
    private List<ArticleBean> mDatas = new ArrayList<>();
    private HomeAdapter mHomeAdapter;
    //当前页码 0开始
    private int mPage;



    @Override
    protected int setContentView() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        mHomeAdapter = new HomeAdapter(mContext, mDatas, 0);
        recyclerView.setAdapter(mHomeAdapter);
        recyclerView.setCanLoadMore(true);
        recyclerView.setListener(new LoadMoreRecycleView.OnFooterAutoLoadMoreListener() {
            @Override
            public void loadMore() {
                recyclerView.showLoadMore();
                mPage ++ ;
                mHomePresenter.getHomeList();
            }

            @Override
            public void reLoadMore() {

            }
        });
        addHeadView();
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mPage = 0;
                mHomePresenter.getHomeList();
                ToastUtil.showToast("刷新数据！！");
                refreshLayout.setRefreshing(false);
            }
        });
    }

    private void addHeadView() {
        View headView = LayoutInflater.from(mContext).inflate(R.layout.main_header_banner, recyclerView, false);
        mBannerView = headView.findViewById(R.id.bannerView);
        recyclerView.addHeadView(headView);
    }

    @Override
    protected void initData() {
        mHomePresenter.getHomeList();
    }


    @Override
    public void showBannerList(final List<BannerBean> result) {
        Log.e(TAG, "showBannerList: 设置轮播图" + result.get(0).getImagePath());
        //设置轮播图
        BannerAdapter mBannerAdapter = new BannerAdapter() {
            @Override
            public View getView(int position, View convertView) {
                BannerBean bannerBean = result.get(position);
                convertView = LayoutInflater.from(mContext).inflate(R.layout.banner_item,null);
                ImageView pic = convertView.findViewById(R.id.iv_banner_pic);
                ImageLoadUtil.load(mContext,bannerBean.getImagePath(),pic);
                return convertView;
            }

            @Override
            public int getCount() {
                return result.size();
            }

            @Override
            public String getBannerDesc(int position) {
                return result.get(position).getTitle();
            }
        };
        mBannerView.setAdapter(mBannerAdapter);
        mBannerView.startRoll();
    }

    @Override
    public void showDataList(List<ArticleBean> result) {
        Log.e(TAG, "showDataList: 显示列表数据" );
        recyclerView.removeLoadMoreView();
        if(mPage == 0){
            mDatas.addAll(result);
            mHomeAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public int getPage() {
        return mPage;
    }


    @Override
    public void showLoading() {
        containerLayout.showLoding();
    }

    @Override
    public void showError() {
        containerLayout.showError();
    }

    @Override
    public void showEmpty() {
        containerLayout.showEmpty();
    }
}
